package com.zhaofei.webtong;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.widget.LinearLayout;

import com.tencent.smtt.sdk.CookieManager;
import com.tencent.smtt.sdk.CookieSyncManager;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2019/1/9
 * MainActivity 首页
 */
public class MainActivity extends AppCompatActivity {
    LinearLayout webView_ll;
    public WebView webView;
    public WebSettings webSettings;
    private MyWebChromeClient mWebChromeClient;

    //    http://sp.zgmycy.cn/api/api/getBootsImage
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView_ll = findViewById(R.id.webView_ll);
        initview();
    }

    private void initview() {
        mWebChromeClient = new MyWebChromeClient(this);
        ThreadManager.getThreadManagerInstance().execute(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        webView = new WebView(MainActivity.this, null);
                        webView_ll.addView(webView, new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.MATCH_PARENT));

                        webSettings = webView.getSettings();
                        webSettings.setJavaScriptEnabled(true);
                        webSettings.setDatabaseEnabled(true);
                        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
                        webSettings.setUseWideViewPort(true);
                        webSettings.setLoadWithOverviewMode(true);
                        webSettings.setDefaultTextEncodingName("UTF -8");
                        webSettings.setSavePassword(true);
                        webSettings.setBuiltInZoomControls(true);
                        webSettings.setDomStorageEnabled(true);
                        webSettings.setSupportZoom(false);
                        webSettings.setAllowFileAccess(true);
                        webSettings.setAllowContentAccess(true);
                        webSettings. setLoadsImagesAutomatically(true);
                        webSettings.setAllowFileAccessFromFileURLs(true);
                        webSettings.setAllowUniversalAccessFromFileURLs(true);
                        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
//                        webView.addJavascriptInterface(new MyJavaFace(), "getPhoto");
//                        webView.setWebChromeClient(new WebChromeClient());
                        webView.setWebChromeClient(mWebChromeClient);
                        webView.setWebViewClient(new WebViewClient() {
                            @Override
                            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                if (url.contains("tel")) {
                                    Intent intent = new Intent(Intent.ACTION_DIAL);
                                    intent.setData(Uri.parse(url));
                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(intent);
                                    return true;
                                }else {
                                    Log.e("Url", url+"");
                                }
                                return super.shouldOverrideUrlLoading(view, url);
                            }


                        });
//                        webView.loadUrl("http://sp.zgmycy.cn/mobile/login/login");
                        webView.loadUrl("http://dbs.zgmycy.cn/");

                    }
                });
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView != null && webView.canGoBack()) {
            webView.goBack();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mWebChromeClient.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        String cookie = cookieManager.getCookie(webView.getUrl());//从H5获取cookie
        Log.i("Activity", "onPageFinished cookie :" + cookie);
        CookieSyncManager.getInstance().sync();
        super.onDestroy();
    }

    class MyJavaFace implements JavascriptInterface {
        @Override
        public Class<? extends Annotation> annotationType() {
            return null;
        }
    }
}